package com.steel.yfmall.cloud.assistant.logAspect;

import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.assistant.po.view.CallZgLogBean;
import com.steel.yfmall.cloud.assistant.utils.constants.EnumZgAPINames;
import com.steel.yfmall.cloud.assistant.view.mapper.CallZgLogBeanMapper;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 日志记录切点类
 *
 * @author 龙飞
 * @version 1.0
 * @since 2020/12/24 星期四 10:23
 */
@Component
@Aspect
@Slf4j
public class CallZgLogAspect {

    @Pointcut(value = "execution(* com.steel.yfmall.cloud.assistant.utils.zg.ZgCallServiceUtils.commonCallZgSoApi(..))")
    public void zgCallServiceUtilsPointCut() {
    }

    @Around(value = "zgCallServiceUtilsPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        CallZgLogBean callZgLogBean = new CallZgLogBean();
        Object[] args = joinPoint.getArgs();
        //获取中冠接口调用参数
        String paraText = JSONObject.toJSONString(args[0]);
        callZgLogBean.setParaText(paraText);
        //调用中冠接口标识
        ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO = (ZgCallWsdlAPIBaseDTO) args[1];
        String interfaceName = zgCallWsdlAPIBaseDTO.getInterfaceName();

        APIMsgBean result = null;
        callZgLogBean.setApiUrl(zgCallWsdlAPIBaseDTO.getUrl());
        callZgLogBean.setParameterType(zgCallWsdlAPIBaseDTO.getParameterType());
        callZgLogBean.setInterfaceName(interfaceName);
        callZgLogBean.setParaKey(zgCallWsdlAPIBaseDTO.getParaKey());
        //只要调通了接口就是1
        callZgLogBean.setStatus(1);
        try {
            //调用中冠接口返回信息
            result = (APIMsgBean) joinPoint.proceed();
            if (1 == result.getSoaStateBean().getCode()) {
                callZgLogBean.setResultText(result.getData().toString());
            } else {
                callZgLogBean.setResultText("<font color=\"red\">" + result.getData().toString() + "</font>");
            }
        } catch (NullPointerException e) {
            callZgLogBean.setStatus(0);
            callZgLogBean.setResultText(result == null || StringUtils.isBlank(result.getSoaStateBean().getMessage()) ? "调用中冠接口时出现异常(返回值为空)" : result.getSoaStateBean().getMessage());
        } catch (Exception e) {
            //网络超时，异常之类的是0
            callZgLogBean.setStatus(0);
            log.error("调用中冠接口【{}】出现异常", interfaceName);
            callZgLogBean.setResultText("调用中冠接口时出现异常(这种情况几乎不可能发生)");
        }
        callZgLogBean.setCreateTime(new Date());
//        callZgLogBean.setUpdateTime();
        // 判断是否需要记录日志
        if(EnumZgAPINames.getEnumApplyType(interfaceName) != null){
            //写入日志库
            callZgLogBeanMapper.insert(callZgLogBean);
        }

        return result;
    }


    @Resource
    private CallZgLogBeanMapper callZgLogBeanMapper;

}
